import rclpy
import threading
import requests
from example_interfaces.msg import String
from queue import Queue

from rclpy.node import Node


class NovalDownload(Node):
    "noval download"

    __threads=[]
    __que=Queue()

    def __init__(self, name):
        super().__init__(name)
        

    def download(self, url, cb):
        print(f'thread:{threading.get_ident}, download:{url}')
        reponse = requests.get(url)
        reponse.encoding = 'utf-8'
        print(f'return:{reponse.status_code}')
        if reponse.status_code == 200:
            cb(url, reponse.text)
        else:
            print(f'download fail, status_code:{reponse.status_code}')


    def start_download(self, url, cb):
        thd = threading.Thread(target=self.download, args=(url, cb))
        self.__threads.append(thd)
        thd.start()

    def finish_cb(self, url, result):
        print(f'{url} finish download, len:{len(result)}, concept{result[:5]}...')
        for line in result.splitlines():
            self.__que.put(line)
            #self.get_logger().info(f'concept:{line}')

    def wait_finish_download(self):
        for thd in self.__threads:
            thd.join()
        self.__threads.clear()
        print(f'all download thread finish')

    def pub(self):
        self.__publisher = self.create_publisher(String, 'noval_pub', 10)
        self.__timer = self.create_timer(1, self.pub_cb)

    def pub_cb(self):
        if self.__que.qsize() > 0 :
            msg = String()
            msg.data = self.__que.get()
            self.get_logger().info(f'concept:{msg.data}')
            self.__publisher.publish(msg)

    def run(self):
        self.start_download("http://localhost:8000/other/noval1.txt", self.finish_cb)
        #self.start_download("http://localhost:8000/other/noval2.txt", self.finish_cb)
        #self.start_download("http://localhost:8000/other/noval3.txt", self.finish_cb)
        self.wait_finish_download()
        self.pub()


def main():
    rclpy.init()
    node = NovalDownload("download_noval")
    node.run()
    rclpy.spin(node)
    rclpy.shutdown
